%This program loads output data generated by Compare_first_trials_behav and
%generates plots for different categories. Choose the folder DLDMCB_trial1_comp under behavioral
%analysis under pooled data

path=uigetdir;
cd(path)

load('resp_classification_firt5.mat');
idx_1=(idx(1:size(behav_clust_param,1)))';
resp_amp_all_1=-1*resp_amp_all(:,1);%negative is in the correct direction
abs_resp_amp_all_1=abs_resp_amp_all(:,1);%negative is in the correct direction
resp_dir_all_1=resp_dir_all(:,1);
resp_time_relcol_1=resp_time_relcol(:,1);
idx_1(isnan(resp_time_relcol_1))=1;%if some no escape trials are miss-classified as escape, correct for that directly
X=categorical({'DL+','DM+','CB+','DL-','DM-','CB-','DL+','DM+','CB+','DL-','DM-','CB-'})
labels=reordercats(X,{'DL+','DM+','CB+','DL-','DM-','CB-'})

x=1:1:12;
%% all response types pooled

% %calculate avergae amplitude
% resp_amp_ave=[mean(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0)),mean(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'>0)),mean(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0)),mean(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0)),mean(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'<0)),mean(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0))]
% resp_amp_ste=[std(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0))/sqrt(sum(Stim_type_all==1&resp_amp_all_1'>0)),std(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'>0))/sqrt(sum(Stim_type_all==2&resp_amp_all_1'>0)),std(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0))/sqrt(sum(Stim_type_all==3&resp_amp_all_1'>0)),std(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0))/sqrt(sum(Stim_type_all==1&resp_amp_all_1'<0)),std(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'<0))/sqrt(sum(Stim_type_all==2&resp_amp_all_1'<0)),std(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0))/sqrt(sum(Stim_type_all==3&resp_amp_all_1'<0))]
% figure;
% bar(labels(1:6),resp_amp_ave)
% hold on
% errorbar(labels(1:6),resp_amp_ave,resp_amp_ste,'.')
% 
% 
% amps=[resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0);-1*resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0)]
% groups=[ones(size(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0)));2*ones(size(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0)))]
% kruskalwallis(amps,groups)
% 
% amps=[resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0);-1*resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0)]
% groups=[ones(size(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0)));2*ones(size(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0)))]
% kruskalwallis(amps,groups)
% %calculate average probability
% 
% resp_prob=[sum(resp_dir_all_1(Stim_type_all==1)>0)/sum(Stim_type_all==1),sum(resp_dir_all_1(Stim_type_all==2)>0)/sum(Stim_type_all==2),sum(resp_dir_all_1(Stim_type_all==3)>0)/sum(Stim_type_all==3),-1*sum(resp_dir_all_1(Stim_type_all==1)<0)/sum(Stim_type_all==1),-1*sum(resp_dir_all_1(Stim_type_all==2)<0)/sum(Stim_type_all==2),-1*sum(resp_dir_all_1(Stim_type_all==3)<0)/sum(Stim_type_all==3)]
% figure;
% bar(labels(7:12),resp_prob)
% 
% %calculate response time relative to collision
% resp_time_ave=[nanmean(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'>0)),nanmean(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'>0)),nanmean(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'>0)),nanmean(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'<0)),nanmean(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'<0)),nanmean(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'<0))]
% resp_time_ste=[nanmean(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'>0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'>0)))),nanmean(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'>0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'>0)))),nanmean(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'>0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'>0)))),nanmean(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'<0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==1&resp_amp_all_1'<0)))),nanmean(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'<0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==2&resp_amp_all_1'<0)))),nanmean(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'>0))/sqrt(sum(~isnan(resp_time_relcol(Stim_type_all==3&resp_amp_all_1'<0))))]
% figure;
% bar(labels(1:6),resp_time_ave)
% hold on
% errorbar(labels(1:6),resp_time_ave,resp_time_ste,'.')

%% calculate average probability
X2=categorical({'DL','CB','DM'})
labels2=reordercats(X2,{'DL','CB','DM'})

resp_prob=[sum(Stim_type_all==1&idx_1==2)/sum(Stim_type_all==1),sum(Stim_type_all==1&idx_1==3)/sum(Stim_type_all==1),sum(Stim_type_all==1&idx_1==4)/sum(Stim_type_all==1),sum(Stim_type_all==1&idx_1==5)/sum(Stim_type_all==1),sum(Stim_type_all==1&idx_1==1)/sum(Stim_type_all==1);sum(Stim_type_all==3&idx_1==2)/sum(Stim_type_all==3),sum(Stim_type_all==3&idx_1==3)/sum(Stim_type_all==3),sum(Stim_type_all==3&idx_1==4)/sum(Stim_type_all==3),sum(Stim_type_all==3&idx_1==5)/sum(Stim_type_all==3),sum(Stim_type_all==3&idx_1==1)/sum(Stim_type_all==3);sum(Stim_type_all==2&idx_1==2)/sum(Stim_type_all==2),sum(Stim_type_all==2&idx_1==3)/sum(Stim_type_all==2),sum(Stim_type_all==2&idx_1==4)/sum(Stim_type_all==2),sum(Stim_type_all==2&idx_1==5)/sum(Stim_type_all==2),sum(Stim_type_all==2&idx_1==1)/sum(Stim_type_all==2)]
figure;
bar(labels2,resp_prob,'stacked')

%calculate response time relative to collision
resp_time_ave=[nanmean(resp_time_relcol_1(Stim_type_all==1)),nanmean(resp_time_relcol_1(Stim_type_all==2)),nanmean(resp_time_relcol_1(Stim_type_all==3))]
resp_time_ste=[nanmean(resp_time_relcol_1(Stim_type_all==1))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==1)))),nanmean(resp_time_relcol_1(Stim_type_all==2))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==2)))),nanmean(resp_time_relcol_1(Stim_type_all==3))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==3))))]
figure;
bar(labels2,resp_time_ave)
hold on
errorbar(labels2,resp_time_ave,resp_time_ste,'.')

resp_time=[(resp_time_relcol_1(Stim_type_all==1));(resp_time_relcol_1(Stim_type_all==3));(resp_time_relcol_1(Stim_type_all==2))]
groups=[ones(size((resp_time_relcol_1(Stim_type_all==1))));2*ones(size((resp_time_relcol_1(Stim_type_all==3))));3*ones(size((resp_time_relcol_1(Stim_type_all==2))))]
[a,b,STATS]=kruskalwallis(resp_time,groups)
figure;multcompare(STATS)

resp_prob=[sum(Stim_type_all'==1&~isnan(resp_time_relcol_1))/sum(Stim_type_all'==1),sum(Stim_type_all'==3 & ~isnan(resp_time_relcol_1))/sum(Stim_type_all'==3),sum(Stim_type_all'==2&~isnan(resp_time_relcol_1))/sum(Stim_type_all'==2)]
figure;
bar(labels2,resp_prob)

abs_resp_amp=[abs_resp_amp_all_1(Stim_type_all==1);abs_resp_amp_all_1(Stim_type_all==3);abs_resp_amp_all_1(Stim_type_all==2)]
group=[ones(size(abs_resp_amp_all_1(Stim_type_all==1)));2*ones(size(abs_resp_amp_all_1(Stim_type_all==3)));3*ones(size(abs_resp_amp_all_1(Stim_type_all==2)))];
[a,b,STATS]=kruskalwallis(abs_resp_amp,groups)
figure
multcompare(STATS)


pcent_cor=[sum(resp_dir_all_1(Stim_type_all==1)>0)/sum(~isnan(resp_dir_all_1(Stim_type_all==1)));sum(resp_dir_all_1(Stim_type_all==3)>0)/sum(~isnan(resp_dir_all_1(Stim_type_all==3)));sum(resp_dir_all_1(Stim_type_all==2)>0)/sum(~isnan(resp_dir_all_1(Stim_type_all==2)))]
figure
bar(labels2,pcent_cor)


% %% do the same but look at different response types
% %% 2:bidir,3 uni corr dir, 4: uni wrong dir, 5: swim,1: no-escape
% rind=5;
% %calculate avergae amplitude
% resp_amp_ave=[mean(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0&idx_1==rind)),mean(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'>0&idx_1==rind)),mean(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0&idx_1==rind)),mean(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0&idx_1==rind)),mean(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'<0&idx_1==rind)),mean(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0&idx_1==rind))]
% resp_amp_ste=[std(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'>0&idx_1==rind))/sqrt(sum(Stim_type_all==1&resp_amp_all_1'>0&idx_1==rind)),std(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'>0&idx_1==rind))/sqrt(sum(Stim_type_all==2&resp_amp_all_1'>0&idx_1==rind)),std(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'>0&idx_1==rind))/sqrt(sum(Stim_type_all==3&resp_amp_all_1'>0&idx_1==rind)),std(resp_amp_all_1(Stim_type_all==1&resp_amp_all_1'<0&idx_1==rind))/sqrt(sum(Stim_type_all==1&resp_amp_all_1'<0&idx_1==rind)),std(resp_amp_all_1(Stim_type_all==2&resp_amp_all_1'<0&idx_1==rind))/sqrt(sum(Stim_type_all==2&resp_amp_all_1'<0&idx_1==rind)),std(resp_amp_all_1(Stim_type_all==3&resp_amp_all_1'<0&idx_1==rind))/sqrt(sum(Stim_type_all==3&resp_amp_all_1'<0&idx_1==rind))]
% figure;
% bar(labels(1:6),resp_amp_ave)
% hold on
% errorbar(labels(1:6),resp_amp_ave,resp_amp_ste,'.')
% 
% resp_time_ave=[nanmean(resp_time_relcol_1(Stim_type_all==1)),nanmean(resp_time_relcol_1(Stim_type_all==2)),nanmean(resp_time_relcol_1(Stim_type_all==3))]
% resp_time_ste=[nanmean(resp_time_relcol_1(Stim_type_all==1))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==1)))),nanmean(resp_time_relcol_1(Stim_type_all==2))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==2)))),nanmean(resp_time_relcol_1(Stim_type_all==3))/sqrt(sum(~isnan(resp_time_relcol_1(Stim_type_all==3))))]
% figure;
% bar(labels(1:3),resp_time_ave)
% hold on
% errorbar(labels(1:3),resp_time_ave,resp_time_ste,'.')
% 
% 



